package agency.tango.materialintro.slice;

import agency.tango.materialintroscreen.SlideComponentContainer;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.security.SystemPermission;

import agency.tango.materialintro.ResourceTable;
import agency.tango.materialintroscreen.MessageButtonBehaviour;
import agency.tango.materialintroscreen.SlideViewBuilder;
import agency.tango.materialintroscreen.slice.MaterialIntroSlice;
import agency.tango.materialintroscreen.utils.ToastUtil;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintro.slice
 * @ClassName: IntroSlice
 * @Description: 主页面
 * @CreateDate: 2021/4/12 14:34
 * @UpdateDate: 2021/4/12 14:34
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */
public class IntroSlice extends MaterialIntroSlice {
    private boolean isFirst = true; // 禁止多次添加 否则messageButtonBehaviours.put
    // 的时候位置错乱 数组下标越界崩溃

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        if (isFirst) {
            enableLastSlideAlphaExitTransition(true);
            getBackButtonTranslationWrapper().setEnterTranslation(Component::setAlpha);
            addOrganizePage();
            addGoOnPage();
            addCustomPage();
            addPermissionPage();
            addEndPage();
            isFirst = false;
        }
    }

    private void addCustomPage() {
        CustomSlide slideFragment = new CustomSlide(getContext());
        addSlide(slideFragment);
    }

    private void addEndPage() {
        SlideComponentContainer endView = new SlideViewBuilder()
                .backgroundColor(ResourceTable.Color_fourth_slide_background)
                .buttonsColor(ResourceTable.Color_fourth_slide_buttons)
                .title(getContext().getString(ResourceTable.String_end_title))
                .description(getContext().getString(ResourceTable.String_end_description))
                .build(getContext());
        addSlide(endView);
    }

    private void addPermissionPage() {
        String[] possiblePermissions = {SystemPermission.INTERNET,
                SystemPermission.GET_WIFI_INFO};
        String[] neededPermissions = {SystemPermission.CAMERA,
                SystemPermission.LOCATION,
                SystemPermission.WRITE_CALENDAR};
        SlideComponentContainer permissionView = new SlideViewBuilder()
                .backgroundColor(ResourceTable.Color_third_slide_background)
                .buttonsColor(ResourceTable.Color_third_slide_buttons)
                .possiblePermissions(possiblePermissions)
                .neededPermissions(neededPermissions)
                .image(ResourceTable.Media_img_equipment)
                .title(getContext().getString(ResourceTable.String_permission_title))
                .description(getContext().getString(ResourceTable.String_permission_description))
                .build(getContext());
        MessageButtonBehaviour permissionButton =
                new MessageButtonBehaviour(view ->
                        showMessage(getContext().getString(ResourceTable.String_permission_button_message)),
                        getContext().getString(ResourceTable.String_permission_button_text));
        addSlide(permissionView, permissionButton);
    }

    private void addGoOnPage() {
        SlideComponentContainer goOnView = new SlideViewBuilder()
                .backgroundColor(ResourceTable.Color_second_slide_background)
                .buttonsColor(ResourceTable.Color_second_slide_buttons)
                .title(getContext().getString(ResourceTable.String_go_on_title))
                .description(getContext().getString(ResourceTable.String_go_on_description))
                .build(getContext());
        addSlide(goOnView);
    }

    private void addOrganizePage() {
        SlideComponentContainer organizeView = new SlideViewBuilder()
                .backgroundColor(ResourceTable.Color_first_slide_background)
                .buttonsColor(ResourceTable.Color_first_slide_buttons)
                .image(ResourceTable.Media_img_office)
                .title(getContext().getString(ResourceTable.String_organize_title))
                .description(getContext().getString(ResourceTable.String_organize_content))
                .build(getContext());
        MessageButtonBehaviour organizeButton =
                new MessageButtonBehaviour(view ->
                        showMessage(getContext().getString(ResourceTable.String_organize_button_message)),
                        getContext().getString(ResourceTable.String_organize_button_text));
        addSlide(organizeView, organizeButton);
    }

    @Override
    public void onFinish() {
        super.onFinish();
        ToastUtil.showToast(this, getContext().getString(ResourceTable.String_close_text));
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("agency.tango.materialintro")
                .withAbilityName("agency.tango.materialintro.MainAbility")
                .build();
        secondIntent.setOperation(operation);
        startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
    }
}
